package com.linln.admin.withdrawal;

import com.linln.common.vo.JsonResult;
import com.linln.common.vo.PageResult;
import com.linln.component.shiro.ShiroUtil;
import com.linln.modules.ticket.service.TicketService;
import com.linln.modules.withdrawal.service.WithdrawalService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.security.auth.Subject;
import java.util.Optional;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.admin.withdrawal
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.admin.withdrawal
 * @Description: TODO
 * @date Date : 2020年12月30日 16:21
 */
@Controller()
@RequestMapping("withdrawal")
public class WithdrawalController {

    @Autowired
    private WithdrawalService withdrawalService;
    @Autowired
    private TicketService ticketService;

    @GetMapping("index")
    public String index() {
        return "/withdrawal/index";
    }

    @ResponseBody
    @GetMapping
    public PageResult getWithdrawalList(@RequestParam(required = false, defaultValue = "0") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit
            , String shopName, String phone, Integer status) {
        return withdrawalService.getLayuiWithdrawal(page, limit, shopName, phone, status);
    }

    @ResponseBody
    @GetMapping("/ticketLog/{shopId}")
    public PageResult getTicketLogList(@RequestParam(required = false, defaultValue = "0") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit
            , @PathVariable("shopId") String shopId, String date) {
        String startDate = null;
        String endDate = null;
        if (!Strings.isBlank(date)) {
            String[] dates = date.split(" - ");
            startDate = dates[0];
            endDate = dates[1];
        }
        return ticketService.getTicketLogList(page, limit, shopId, startDate, endDate);
    }


    //更新审核状态
    @ResponseBody
    @PostMapping("/updateExamineStatus")
    public JsonResult updateExamineStatus(@RequestParam("id") String id, @RequestParam("status") Integer status, @RequestParam(value = "refuseReason", required = false) String refuseReason) {
        return withdrawalService.updateExamineStatus(id, status, refuseReason, ShiroUtil.getSubject().getId()) ? JsonResult.ok() : JsonResult.error();
    }


    //更新转账状态
    @ResponseBody
    @PostMapping("/updateTransferStatus")
    public JsonResult updateTransferStatus(@RequestParam("id") String id, @RequestParam("file") MultipartFile file) {
        return withdrawalService.adoptTransfer(id, file) ? JsonResult.ok() : JsonResult.error();
    }

}
